package com.play.ballen.home.dialog

import android.app.Activity
import android.app.Dialog
import android.content.Context
import android.graphics.drawable.ColorDrawable
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.view.inputmethod.InputMethodManager
import android.widget.CheckBox
import android.widget.RelativeLayout
import android.widget.TextView
import com.play.ballen.R
import com.play.ballen.widget.emoji.FaceTextUtil
import org.kymjs.chat.OnOperationListener
import org.kymjs.chat.bean.Emojicon
import org.kymjs.chat.bean.Faceicon
import org.kymjs.chat.emoji.DisplayRules
import org.kymjs.chat.widget.KJChatKeyboard
import java.io.File

class CommentDialogUtils(private val mContext: Activity) {

    val LAYOUT_TYPE_HIDE = 0
    val LAYOUT_TYPE_FACE = 1
    val LAYOUT_TYPE_MORE = 2
    private var layoutType: Int = LAYOUT_TYPE_HIDE
    val dialog: Dialog = Dialog(mContext)
    private lateinit var contentView: View
    private lateinit var send: TextView
    private var mRlFace: RelativeLayout? = null
    private lateinit var mBtnFace: CheckBox
    private lateinit  var box: KJChatKeyboard
    var rootView: ViewGroup? = null
    private val mInputMethodManager by lazy {
        mContext.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    }
    var valuesCallback: ((content: String) -> Unit)? = null
    fun show() {
        if (!this::contentView.isInitialized) {
            contentView = LayoutInflater.from(mContext).inflate(R.layout.fragment_dialog_comment, null)

            contentView.let {
                box = it.findViewById(org.kymjs.chat.R.id.chat_msg_input_box)
                box.onOperationListener = object : OnOperationListener {
                    override fun send(content: String) {
                          valuesCallback?.invoke(content)
                    }

                    override fun selectedFace(content: Faceicon) {

                    }

                    override fun selectedEmoji(str: String?, faceImageRes: Int) {
                        TODO("Not yet implemented")
//                        box.editTextBox.append(FaceTextUtil.getFaceImageSpan(mContext,str, faceImageRes))
                    }

//                    override fun selectedEmoji(emoji: Emojicon) {
//
//                    }

                    override fun selectedBackSpace(back: Emojicon) {
                        DisplayRules.backspace(box.editTextBox)
                    }

                    override fun selectedFunction(index: Int) {

                    }
                }
                dialog.setContentView(it)
                dialog.setCanceledOnTouchOutside(true)
                dialog.window?.setLayout(
                    WindowManager.LayoutParams.MATCH_PARENT,
                    WindowManager.LayoutParams.WRAP_CONTENT
                )
                val params = dialog.window?.attributes
                params?.gravity = Gravity.BOTTOM
                dialog.window?.attributes = params
                dialog.window?.setBackgroundDrawable(ColorDrawable())
                dialog.window?.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
                dialog.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE or WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE)

                val faceCagegory: MutableList<String> = ArrayList()
                val faceList = File("")
                if (faceList.isDirectory) {
                    val faceFolderArray = faceList.listFiles()
                    for (folder in faceFolderArray) {
                        if (!folder.isHidden) {
                            faceCagegory.add(folder.absolutePath)
                        }
                    }
                }
                box.setFaceData(faceCagegory)
            }
        }

        if (!dialog.isShowing) {
            dialog.show()
//            tvContent.post {
//                tvContent.requestFocus()
//                mInputMethodManager.showSoftInput(tvContent, InputMethodManager.SHOW_IMPLICIT)
//            }
        }
    }



}